package es.cea.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@WebServlet("/Libros")
public class Libros extends HttpServlet {
	private static final long serialVersionUID = 1L;       
    
    public Libros() {
        super();
        
         
        
    }

	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		 PrintWriter writer = response.getWriter();	    
		 writer.print("<html><body>");
	        try  
	        {  
	        	Class.forName("com.mysql.jdbc.Driver");  
	        	Connection conexion = DriverManager.getConnection ("jdbc:mysql://localhost/libreria","root", "root");        
	        	Statement s = conexion.createStatement();
	        	ResultSet rs = s.executeQuery("SELECT * FROM libros");	        	
	        	
	        	while(rs.next()){
	        		Libro libro = new Libro(rs.getInt(1),rs.getString(2));
	        		System.out.println(libro);
	        		writer.write(libro.getNombre());	        		
	        		writer.write("<a href='./LibroDetail?id="+libro.getId() +"'>Detallar</a>");	
	        		writer.write("<a href='./LibroEdit?id="+libro.getId()+"'>Editar</a><br>");
	        	}	        	
	        	writer.print("</body></html>");
	        	
	        } catch (Exception e)  
	        {  
	           e.printStackTrace();  
	        } 
	        
	        Set<stat> estadisticas = (HashSet<stat>) request.getSession().getAttribute("estadisticas");
	        stat aux = new stat("Libros",0);
	        if(estadisticas.contains(aux)){
	        	estadisticas.add(new stat("Libros",aux.cont+1));
	        }	        	
	        else{
	        	estadisticas.add(aux);
	        }
	        	
	        	
	        	
	}
}
